#include "level.h"

Level::Level(int lvl, QString str)
{
    filenameBase = str;
    filenameBase.replace("/debug","");
    changeLevel(lvl);
}

void Level::changeLevel(int newLvl){

    QString loopCntS, xS,yS,wS,hS;
    int loopCnt, x,y,w,h;
    switch(newLvl){
        case 1: //loads level one
        {
           filename = filenameBase +"/Levels/lvl1.txt" ;
           break;
        }
        case 2: //loads level two
        {
           filename = filenameBase +"/Levels/lvl2.txt" ;
           break;
        }
        case 3: //loads level three
        {
           filename = filenameBase +"/Levels/lvl3.txt" ;
           break;
        }
        case 4: //loads level four
        {
           filename = filenameBase +"/Levels/lvl4.txt" ;
           break;
        }
        case 5: //loads level five
        {
           filename = filenameBase +"/Levels/lvl5.txt" ;
           break;
        }
        case 6: //loads level six
        {
           filename = filenameBase +"/Levels/lvl6.txt" ;
           break;
        }
        case 7: //loads level seven
        {
           filename = filenameBase +"/Levels/lvl7.txt" ;
           break;
        }
        case 8: //loads level eight
        {
           filename = filenameBase +"/Levels/lvl8.txt" ;
           break;
        }
        case 9: //loads level nine
        {
           filename = filenameBase +"/Levels/lvl9.txt" ;
           break;
        }
        case 10: //loads level ten
        {
           filename = filenameBase +"/Levels/lvl10.txt" ;
           break;
        }
         default:
           filename = filenameBase +"/Levels/lvl1.txt";
           break;
        }

    QFile file(filename);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);

    loopCntS = in.readLine();
    loopCnt = loopCntS.toInt();
    boxes.resize(loopCnt);
    for(int i = 0; i < loopCnt; ++i){ //reads in boxes
        xS = in.readLine();
        yS = in.readLine();
        wS = in.readLine();
        hS = in.readLine();

        x = xS.toInt();
        y = yS.toInt();
        w = wS.toInt();
        h = hS.toInt();
        Box *b = new Box(x,y,w,h);
        boxes[i] = b;
    }
    loopCntS = in.readLine();
    loopCnt = loopCntS.toInt();
    spikes.resize(loopCnt);
    for(int i = 0; i < loopCnt; ++i){  //reads in spikes
        xS = in.readLine();
        yS = in.readLine();
        wS = in.readLine();
        hS = in.readLine();

        x = xS.toInt();
        y = yS.toInt();
        w = wS.toInt();
        h = hS.toInt();
        Box *b = new Box(x,y,w,h);
        spikes[i] = b;
    }
    file.close();
}

int Level::getRW(int x, int y){ //finds the closest right wall
    wall = 800;
    for(int i = 0; i < boxes.size(); ++i){
        Box *b =boxes[i];
        int X = b->getX();
        if(x+40 < X+20 && y > b->getY()-41 && y < b->getY()+b->getH()-1){
            if(X < wall)
                wall = X;
        }
    }
    return wall;
}

int Level::getLW(int x, int y){ //finds the closest left wall
    wall = 0;
    for(int i = 0; i < boxes.size(); ++i){
        Box *b = boxes[i];
        int X = b->getX()+b->getW();
        if(x >= X-40 && y > b->getY()-41 && y < b->getY()+b->getH()-1){
            if(X > wall)
                wall = X ;
        }
    }
    return wall;
}

int Level::getFlY(int x, int y){  //finds the closest floor for y calculations
    wall = 1100;
    for(int i =0; i<boxes.size(); ++i){
        Box *b =boxes[i];
        int X = b->getX();
        if(x > X-28 && x < X+b->getW()-12 && y <= b->getY()){
            if(b->getY() < wall)
                wall = b->getY();
        }
    }
    return wall;
}

int Level::getFlX(int x, int y){ //finds the closest floor for x calculations
    wall = 1100;
    for(int i = 0; i < boxes.size(); ++i){
        Box *b =boxes[i];
        int X = b->getX();
        if(x > X-40 && x < X+b->getW() && y <= b->getY()){
            if(b->getY() < wall)
                wall = b->getY();
        }
    }
    return wall;
}

int Level::getTop(int x, int y){ //finds the closest ceiling
    wall =0;
    for(int i = 0; i < boxes.size(); ++i){
        Box *b = boxes[i];
        int X = b->getX();
        if(x > X-28 && x < X+b->getW()-12 && y >= b->getY()+b->getH()-1){
            if( b->getY()+b->getH() > wall)
                wall = b->getY()+b->getH();
        }
    }
    return wall;
}

bool Level::getSpikes(int x, int y){
    bool bl = false;
    for(int i = 0; i < spikes.size(); ++i){
        Box *b = spikes[i];
        int X = b->getX();
        if(x > X-25 && x < X+b->getW()-15 && y > b->getY()-35 && y < b->getY()+b->getH()-5){
            bl = true;
        }
    }
    return bl;
}
